
require('reflect-metadata');
const { DataSource } = require('typeorm');
const port: number = parseInt(process.env.MYSQL_PORT) || 3306;
export const dataSource = new DataSource({
  type: 'mysql',
  host: process.env.MYSQL_HOST || '127.0.0.1',
  port: port,
  username: 'root',
  password: '123456',
  database: 'jd_db',
  logging: false,
  synchronize: true,  //连接数据库时会根据实体类的定义自动同步数据库架构
  entities: ["src/entity/*.ts"],
  extra: {
    authPlugin: 'mysql_native_password',
  }
});

export const connectionDataBase = async function() {
  try {
    await dataSource.initialize();
  } catch (error) {
    console.log('数据库连接失败', error.message);
  }
};